<?php
/**
 * Created by PhpStorm.
 * User: jimmyhsu
 * Date: 2016/10/9
 * Time: 下午12:59
 */
include ("conn.php");
$username = $_POST["user"];
$deviceid = $_POST["deviceid"];
$checkSql = "select * from userinfo where name = '$username' and deviceid = '$deviceid'";
$checkRs = mysql_query($checkSql);
if (mysql_num_rows($checkRs) == 0) {
    die("invalid visit");
}

if (""!=$_POST['targetuser']) {
	$targetUser = $_POST['targetuser'];
    $sql = "select * from secondhandtb where username='$targetUser' order by id DESC";
    $rsItems = new SecondHandSearchItems();
    $rsItems->status = 200;
    $rsItems->hasMore = false;
    $rs = mysql_query($sql);
    while ($row = mysql_fetch_array($rs)){
        array_push($rsItems->items, array("id"=>$row["id"], "touxiang"=>urlencode($row["userimage"]), "name"=>urlencode($row["username"]),
            "time"=>$row["time"], "title"=>urlencode($row["title"]),"itemimg"=>urlencode($row["imageurls"]), "price"=>$row["price"]));
    }
    echo urldecode(json_encode($rsItems));
    die();
}
$pageNumber = $_POST["page"];
$page_offset = 0;
$page_size = 20;
$sql = "select * from secondhandtb";
$rs = mysql_query($sql) or die("cannnot connect to db");
$rsLineCount = mysql_num_rows($rs);
$offset = ($pageNumber - 1) * $page_size;
$rsItems = new SecondHandSearchItems();
if (($pageNumber - 1) * $page_size > $rsLineCount) {
    $rsItems->hasMore = false;
    $rsItems->status = 201;
    echo json_encode($rsItems);
    die();
}else if ($pageNumber * $page_size < $rsLineCount) {
    $sql = "select * from secondhandtb order by id desc limit $offset,$page_size";
    $rs = mysql_query($sql) or die("inner cannnot connect to db");
    $rsItems->hasMore = true;
}else{
    $remains = $rsLineCount - $offset;
    $sql = "select * from secondhandtb order by id desc limit $offset,$remains";
    $rs = mysql_query($sql) or die("inner cannnot connect to db");
    $rsItems->hasMore = false;
}
$rsItems->status = 200;
mysql_data_seek($rs,0);
while ($row = mysql_fetch_array($rs)){
    array_push($rsItems->items, array("id"=>$row["id"], "touxiang"=>urlencode($row["userimage"]), "name"=>urlencode($row["username"]),
        "time"=>$row["time"], "title"=>urlencode($row["title"]),"itemimg"=>$row["imageurls"], "price"=>$row["price"]));
}
echo urldecode(json_encode($rsItems));
/**
 * Class SecondHandSearchItems
 * class ViewHolder {
public ImageView mTouXiang;
public TextView mNickname;
public TextView mTime;
public ImageView mItemIv;
public TextView mPriceTv;
}
 */

class SecondHandSearchItems {
    public $status;
    public $hasMore;
    public $items = array();
}
